package com.qqtop10.manager.controller;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.qqtop10.manager.model.Admin;
import com.qqtop10.manager.model.Coupon;
import com.qqtop10.manager.model.CouponShop;
import com.qqtop10.manager.model.CouponShopAdd;
import com.qqtop10.manager.model.CouponUser;
import com.qqtop10.manager.model.PageCoupon;
import com.qqtop10.manager.model.PlatFormCouponDetails;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.service.CouponService;
import com.qqtop10.manager.utils.LogHelper;
import com.qqtop10.manager.utils.RandomUtils;

@RequestMapping("/manager/coupon/")
@Controller
public class CouponController extends BaseController {
	@Autowired
	@Qualifier("couponService")
	private CouponService couponService;

	// 查看所有的优惠券基本信息
	@RequestMapping("getAllCoupon.do")
	@ResponseBody
	public ModelMap getAllCoupon(HttpServletRequest request, HttpServletResponse response, PageCoupon pageCoupon)
			throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		ModelMap map = new ModelMap();
		List<Coupon> list = couponService.getAllCoupon(pageCoupon);
		if (list == null || list.size() == 0) {
			list = new ArrayList<>();
			map.put("data", list);
			map.put("msg", "暂无优惠券信息");

		} else {
			map.put("data", list);
		}
		return map;
	}

	// 查看用户关联的优惠券基本信息
	@RequestMapping("getUserCoupon.do")
	@ResponseBody
	public List<CouponUser> getUserCoupon(HttpServletRequest request, HttpServletResponse response,
			PageCoupon pageCoupon) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		List<CouponUser> list = couponService.getUserCoupon(pageCoupon);
		return list;
	}

	// 查看店铺关联的优惠券基本信息
	@RequestMapping("getShopCoupon.do")
	@ResponseBody
	public List<CouponShop> getShopCoupon(HttpServletRequest request, HttpServletResponse response,
			PageCoupon pageCoupon) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		List<CouponShop> list = couponService.getShopCoupon(pageCoupon);
		return list;
	}

	// 获取一张优惠券信息
	@RequestMapping("getOneCoupon.do")
	@ResponseBody
	public Coupon getOneCouponByUUID(HttpServletRequest request, HttpServletResponse response, String uuid)
			throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		return couponService.getOneCouponByUUID(uuid);
	}

	@RequestMapping("addCouponInfo.do")
	@ResponseBody
	public Result addCouponInfo(HttpServletRequest request, HttpServletResponse response, CouponShopAdd coupon)
			throws ParseException, UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
		Date startDate = sdf.parse(coupon.getStartDate());
		Date endDate = sdf.parse(coupon.getEndDate());
		if (startDate.after(endDate)) {
			return err("开始时间不能大于结束时间");
		}
		String couponName = (coupon.getCouponName());
		coupon.setCouponName(couponName);
		coupon.setUuid(RandomUtils.makeUUID());
		couponService.addCouponInfo(coupon);
		Admin Tempadmin = (Admin) getRequest().getSession().getAttribute("Logind");
		LogHelper.Log(Tempadmin.getAccount(), "后台优惠券管理模块", "添加成功", LogHelper.getLocalIp(getRequest()),
				Tempadmin.getAccount(), "", true);

		return ok("操作成功");
	}

	// 删除优惠券信息
	@RequestMapping("deleteCoupon.do")
	@ResponseBody
	public Result deleteCoupon(Integer ids) {
		String uuid = couponService.selectUUIDByID(ids);
		if (!StringUtils.isEmpty(uuid)) {
			int res1 = couponService.deleteById(uuid);
			int res2 = couponService.deleteCouponUserByCouponUUID(uuid);
			if (res1 > 0 || res2 > 0) {
				Admin Tempadmin = (Admin) getRequest().getSession().getAttribute("Logind");
				LogHelper.Log(Tempadmin.getAccount(), "后台优惠券管理模块", "删除成功", LogHelper.getLocalIp(getRequest()),
						Tempadmin.getAccount(), "", true);

				return ok("操作成功");
			} else {
				return err("删除失败");
			}
		}
		return err("删除失败");
	}

	@RequestMapping("platFormCouponDetails.do")
	@ResponseBody
	public List<PlatFormCouponDetails> selectPlatFormCouponDetails(HttpServletRequest request,
			HttpServletResponse response) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		List<PlatFormCouponDetails> list = couponService.selectPlatFormCouponDetails();
		if (list == null || list.size() == 0) {
			list = new ArrayList<>();
		}
		for (PlatFormCouponDetails details : list) {
			if (details.getIsUsed() == null || "".equals(details.getIsUsed()) || "0".equals(details.getIsUsed())) {
				details.setIsUsed("未使用");
			} else {
				details.setIsUsed("已使用");
			}
		}
		return list;
	}
}
